package com.openroom.ui;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentKeyListener;
import org.apache.pivot.wtk.Keyboard.KeyCode;
import org.apache.pivot.wtk.Keyboard.KeyLocation;

import com.openroom.model.ModelLocator;
import com.openroom.model.ModelLocator.ModeFlag.SelectMode;
import com.openroom.model.Photograph;

public class OPMKeyHandler implements ComponentKeyListener {

    @Override
    public boolean keyTyped(Component component, char character) {
        return false;
    }

    @Override
    public boolean keyPressed(Component component, int keyCode,
            KeyLocation keyLocation) {
        Photograph currentlySelectedImage = ModelLocator.getInstance().getCurrentlySelectedImage();
        switch (keyCode) {
        case 17:
            ModelLocator.getInstance().setCurrentSelectMode(SelectMode.MULTIPLE);
            break;
        case KeyCode.DELETE:
            // EventManager.getInstance().publishEvent(new
            // ApplicationEvent(EventType.NEXT_IMAGE));
            break;
        case KeyCode.R:
            if (currentlySelectedImage.isRejected())
            {
                ModelLocator.getInstance().unRejectImage();
            }
            else
            {
                ModelLocator.getInstance().rejectImage();
            }
            break;
        case KeyCode.S:
            if (currentlySelectedImage.isSelected())
            {
                ModelLocator.getInstance().unSelectImage();
            }
            else
            {
                ModelLocator.getInstance().selectImage();
            }
            break;
        case KeyCode.RIGHT:
            ModelLocator.getInstance().nextImage();
            break;
        case KeyCode.LEFT:
            ModelLocator.getInstance().prevImage();
            break;
        case KeyCode.N0:
            ModelLocator.getInstance().changeRating(0);
            break;
        case KeyCode.N1:
            ModelLocator.getInstance().changeRating(1);
            break;
        case KeyCode.N2:
            ModelLocator.getInstance().changeRating(2);
            break;
        case KeyCode.N3:
            ModelLocator.getInstance().changeRating(3);
            break;
        case KeyCode.N4:
            ModelLocator.getInstance().changeRating(4);
            break;
        case KeyCode.N5:
            ModelLocator.getInstance().changeRating(5);
            break;
        default:
            break;
        }
        return false;
    }

    @Override
    public boolean keyReleased(Component component, int keyCode,
            KeyLocation keyLocation) {
        if (keyCode == 17)
            ModelLocator.getInstance().setCurrentSelectMode(SelectMode.SINGLE);
        return false;
    }

}
